package com.devsoft.foodfrenzy;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.opengl.util.GLState;
import org.andengine.util.adt.align.HorizontalAlign;
import org.andengine.util.adt.color.Color;

import com.devsoft.foodfrenzy.SceneManager.SceneType;

public class GameOverScene extends BaseScene implements IOnMenuItemClickListener{
	
	private MenuScene menuChildScene;	//menu scene is imported
	private final int MENU_AGAIN = 0;
	private final int MENU_BACK = 1;
	private Sprite nhs;
	private Sprite nau;
	private Sprite nsu;
	private boolean popup_flag = false;
	private Text finalscoreText;
	
	
	private void createMenuChildScene()
	{
	    menuChildScene = new MenuScene(camera);	//sets up the palette for the screen
	    menuChildScene.setPosition(400, 240);
	    
	    final IMenuItem againMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_AGAIN, resourcesManager.again_region, vbom), 1.2f, 1);
	    final IMenuItem backMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_BACK, resourcesManager.back_region, vbom), 1.2f, 1);
	    
	    menuChildScene.addMenuItem(againMenuItem);	//adds buttons to the menu
	    menuChildScene.addMenuItem(backMenuItem);
	    
	    menuChildScene.buildAnimations();		//builds animation for button press; makes button bigger when pressed
	    menuChildScene.setBackgroundEnabled(false);
	    
	    againMenuItem.setPosition(againMenuItem.getX()-600, againMenuItem.getY()-430);	//sets button locations; need to resituate
	    backMenuItem.setPosition(backMenuItem.getX()-200, backMenuItem.getY()-360);
	    
	    menuChildScene.setOnMenuItemClickListener(this);	//listen for press, act appropriately
	    
	    setChildScene(menuChildScene);
	}
	
	@Override
	public void createScene() 
	{
		UserData.getInstance().init(activity.getBaseContext());
		createBackground();
	    createMenuChildScene();
	    
	    if(!(UserData.getInstance().getMusicPref()))
	    {
			resourcesManager.game_gameover.play();
		}
	    
	    finalscoreText = new Text(20, 300, resourcesManager.big_font, "FinalScore:0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
	    finalscoreText.setAnchorCenter(0, 0);
	    finalscoreText.setColor(Color.RED);
	    finalscoreText.setText("FINAL\nScore:\n" + UserData.getInstance().getLastScore());
	    attachChild(finalscoreText);
	    
	    nhs = new Sprite(600, 240, resourcesManager.nhs_region, vbom);
	    if (UserData.getInstance().checkFlag(0))
        {
        	nhs.setVisible(true);
        	popup_flag = true;
        }
        else{
        	nhs.setVisible(false);
        }
	    attachChild(nhs);
	    
	    nau = new Sprite(200, 240, resourcesManager.nau_region, vbom);
	    if (UserData.getInstance().checkFlag(1))
        {
        	nau.setVisible(true);
        	popup_flag = true;
        }
        else{
        	nau.setVisible(false);
        }
	    attachChild(nau);
	    
	    nsu = new Sprite(400, 240, resourcesManager.nsu_region, vbom);
	    if (UserData.getInstance().getCount() % 2 == 0 && UserData.getInstance().checkFlag(1))
        {
        	nsu.setVisible(true);
        	popup_flag = true;
        }
        else{
        	nsu.setVisible(false);
        }
	    attachChild(nsu);
	    
	    this.registerUpdateHandler(new TimerHandler(2.0f, true, new ITimerCallback() //player animation
	    {
	    	public void onTimePassed(final TimerHandler pTimerHandler)
	        {
	        	if(nhs.isVisible())
	        	{
	        		nhs.setVisible(false);
	        		UserData.getInstance().resetFlag(0);
	        	}
	        	if(nau.isVisible())
	        	{
	        		nau.setVisible(false);
	        		UserData.getInstance().resetFlag(1);
	        	}
	        	if(nsu.isVisible())
	        	{
	        		nsu.setVisible(false);
	        	}
	        	popup_flag = false;
	        }
	    }));
	}
	
	private void createBackground()
	{
	    attachChild(new Sprite(400, 240, resourcesManager.gameover_background_region, vbom)
	    {
	        @Override
	        protected void preDraw(GLState pGLState, Camera pCamera) //makes picture quality smoother
	        {
	            super.preDraw(pGLState, pCamera);
	            pGLState.enableDither();
	            
	        }
	        
	    });
	    
	}

	@Override
	public void onBackKeyPressed() 	//exit app when backed
	{
		System.exit(0);
	}
	

	@Override
	public SceneType getSceneType()
	{
	    return SceneType.SCENE_GAMEOVER;
	}
	
	
	@Override
	public void disposeScene() 
	{
		menuChildScene.detachSelf();
		menuChildScene.dispose();
        this.detachSelf();
        this.dispose();
	}
	
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY)
	{
		switch(pMenuItem.getID())
	    {
			case MENU_AGAIN:
				if(!popup_flag)
				{
					SceneManager.getInstance().loadSPMenuScene(engine);
				}
				return true;
	        case MENU_BACK:
	        	if(!popup_flag)
				{
	        		SceneManager.getInstance().loadMenuScene(engine, this.getSceneType());
				}
	            return true;
	        default:
	            return false;
	            
	    }
		
	}

}